package com.proton.carepatchtemp.fragment;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapter.HealthTipsAdapter;
import com.proton.carepatchtemp.base.BaseFragment;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.database.daomanager.HealthTipManager;
import com.proton.carepatchtemp.database.table.HealthTip;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.MsgCenter;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;


public class TipsMsgFragment extends BaseFragment implements BGARefreshLayout.BGARefreshLayoutDelegate {
    private RecyclerView mRcvTipsMsgList;
    private TextView mTvBitmapTipsMsg;
    private BGARefreshLayout mBgaRflTips;

    private HealthTipsAdapter mTipsAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private List<HealthTip> mHealthTips = new ArrayList<>();
    private BGANormalRefreshViewHolder mBGANormalRefreshViewHolder;

    public static TipsMsgFragment newInstance() {
        return new TipsMsgFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTipsAdapter = new HealthTipsAdapter(mHealthTips, getActivity());
        mLinearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_tips_msg, container, false);
        initView(view);
        mRcvTipsMsgList.setLayoutManager(mLinearLayoutManager);
        mRcvTipsMsgList.setHasFixedSize(true);
        mRcvTipsMsgList.setAdapter(mTipsAdapter);
        mRcvTipsMsgList.setItemAnimator(new DefaultItemAnimator());
        setBgaRefreshLayout();
        mBgaRflTips.beginRefreshing();
        return view;
    }

    private void setBgaRefreshLayout() {
        mBgaRflTips.setDelegate(this);
        mBGANormalRefreshViewHolder = new BGANormalRefreshViewHolder(getActivity(), true);
        mBGANormalRefreshViewHolder.setLoadingMoreText(getString(R.string.loading));
        mBgaRflTips.setRefreshViewHolder(mBGANormalRefreshViewHolder);
    }

    @Override
    public void dealEvent(MainEvent event) {
        switch (event.getId()) {
            case Codes.NET_HEALTH_TIPS_GETUPLIST:
                if (event.isSuccess()) {
                    mHealthTips.clear();
                    mHealthTips.addAll(HealthTipManager.getAll());
                    mTipsAdapter.notifyDataSetChanged();
                    if (mHealthTips.size() > 0) {
                        mTvBitmapTipsMsg.setVisibility(View.GONE);
                    } else {
                        mTvBitmapTipsMsg.setVisibility(View.VISIBLE);
                    }
                } else {
                    TastyUtils.makeTextShort("获取消息失败！请稍后重试！", TastyToast.ERROR);
                }
                mBgaRflTips.endRefreshing();
                mBgaRflTips.endLoadingMore();
                break;
            case Codes.NET_HEALTH_TIPS_GETDOWNLIST:
                if (event.isSuccess()) {
                    if (mHealthTips.size() > 0) {
                        mHealthTips.addAll(HealthTipManager.getBoforeList(mHealthTips.get(mHealthTips.size() - 1).getTime()));
                    } else {
                        mHealthTips.addAll(HealthTipManager.getAll());
                    }
                    mTipsAdapter.notifyDataSetChanged();
                    if (mHealthTips.size() > 0) {
                        mTvBitmapTipsMsg.setVisibility(View.GONE);
                    } else {
                        mTvBitmapTipsMsg.setVisibility(View.VISIBLE);
                    }
                } else {
                    TastyUtils.makeTextShort("获取消息失败！请稍后重试！", TastyToast.ERROR);
                }
                mBgaRflTips.endRefreshing();
                mBgaRflTips.endLoadingMore();
                break;
        }
    }

    private void initView(View view) {
        mRcvTipsMsgList = view.findViewById(R.id.rcv_tips_msg_list);
        mTvBitmapTipsMsg = view.findViewById(R.id.tv_bitmap_tips_msg);
        mBgaRflTips = view.findViewById(R.id.bga_rfl_tips);
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealEvent(event);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        MsgCenter.getHealthTips(0L, 1, 0);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if (mHealthTips.size() <= 0) {
            MsgCenter.getHealthTips(0L, 1, 0);
            return true;
        }
        MsgCenter.getHealthTips(mHealthTips.get(mHealthTips.size() - 1).getTime(), 1, 1);
        return true;
    }
}
